package com.group3.service.impl;

import com.group3.dao.AdminMapper;
import com.group3.dao.StudentMapper;
import com.group3.dao.TeacherMapper;
import com.group3.domain.*;
import com.group3.exception.BusinessException;
import com.group3.exception.ExceptionEnum;
import com.group3.pojo.Login;
import com.group3.utils.Md5;
import com.group3.utils.Result;
import com.group3.service.CommonService;
import com.group3.utils.JWTUtils;
import com.group3.utils.ResultUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommonServiceImpl implements CommonService {
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private AdminMapper adminMapper;

    @Override
    public Result login(Login login) {
        login.setPassword(Md5.encodeByMD5(login.getPassword()));
        String avatar = "";
        String name = "";
        if (login.getType().equals("0")) {//用户类型为0时用户为管理员
            AdminExample adminExample = new AdminExample();
            adminExample
                    .createCriteria()
                    .andAdminAccountEqualTo(login.getUsername())
                    .andPasswordEqualTo(login.getPassword());
            List<Admin> admins = adminMapper.selectByExample(adminExample);
            if (admins.isEmpty()) {//未找到用户名、密码、类型相符的用户
                throw new BusinessException(ExceptionEnum.PASSWORD_ERR);
            }
            name = "管理员";
        } else if (login.getType().equals("1")) {//用户类型为1时用户为教师
            TeacherExample teacherExample = new TeacherExample();
            teacherExample
                    .createCriteria()
                    .andTeacherIdEqualTo(login.getUsername())
                    .andPasswordEqualTo(login.getPassword());
            List<Teacher> teachers = teacherMapper.selectByExample(teacherExample);
            if (teachers.isEmpty()){
                throw new BusinessException(ExceptionEnum.PASSWORD_ERR);
            }
            for (Teacher teacher : teachers){
                name = teacher.getTeacherName();
            }
        }else if (login.getType().equals("2")){//用户类型为2时用户为学生
            StudentExample studentExample = new StudentExample();
            studentExample
                    .createCriteria()
                    .andStudentIdEqualTo(login.getUsername())
                    .andPasswordEqualTo(login.getPassword());
            List<Student> students = studentMapper.selectByExample(studentExample);
            if (students.isEmpty()) {
                throw new BusinessException(ExceptionEnum.PASSWORD_ERR);
            }
            for (Student student : students){
                avatar = student.getAvatorUrl();
                name = student.getStudentName();
            }
        }else {
            throw new BusinessException(ExceptionEnum.USERTYPE_ERR);
        }

        Map<String, String> payload = new HashMap<>();
        payload.put("id", login.getUsername());//前端输入的username应该是用户id
        payload.put("type", login.getType());
        String token = JWTUtils.getToken(payload);

        Map<String, String> data = new HashMap<>();
        data.put("token", token);
        data.put("avatar",avatar);
        data.put("name",name);

        return ResultUtil.success("登录成功", data);

    }

}
